package com.leil.movies.spider;

import com.leil.movies.project.entity.Movie;
import com.leil.movies.project.service.impl.MovieServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class MovieSpiderExecutor {

    @Value("${movie-spider.executor-switch}")
    private boolean executorSwitch;

    ExecutorService threadPool = Executors.newFixedThreadPool(2);

    private List<String> genresList;

    MovieServiceImpl movieService;

    @Autowired
    public void setMovieService(MovieServiceImpl movieService) {
        this.movieService = movieService;
    }

    public MovieSpiderExecutor() {
        this.genresList = Arrays.asList(
                "剧情",
                "喜剧",
                "动作",
                "爱情",
                "科幻",
                "动画",
                "悬疑",
                "惊悚",
                "恐怖",
                "犯罪",
                "同性",
                "音乐",
                "歌舞",
                "传记",
                "历史",
                "战争",
                "西部",
                "奇幻",
                "冒险",
                "灾难",
                "武侠",
                "情色");
    }

    @PostConstruct
    public void init() {
        if (this.executorSwitch) {
            System.err.println("线程池开始执行。。。");
            for (String genres : this.genresList) {
                this.threadPool.execute(new MovieSpiderRunnable(genres, movieService));
                try {
                    Thread.sleep(30000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
